type Game = {
  id: string;
  name: string;
  sortIndex: string;
  type: string;
  tags: string;
  newGame: string;
  classicgames: string;
  hotgame: string;
  updated: string;
  des: string;
  pathPicture: string;
  like: number;
  unlike: number;
  score: number;
  votes: string;
  url: string;
};
const sortGamesByIds = (games: Game[], ids: string[]) => {
  return games.sort((a, b) => {
    const aIndex = ids.indexOf(a.id + ""); // 获取 a 的 id 在 ids 中的索引
    const bIndex = ids.indexOf(b.id + ""); // 获取 b 的 id 在 ids 中的索引
    // 如果 a 的 id 在 ids 中，且 b 的 id 不在 ids 中，a 排在前面
    if (aIndex !== -1 && bIndex === -1) return -1;
    // 如果 b 的 id 在 ids 中，且 a 的 id 不在 ids 中，b 排在前面
    if (bIndex !== -1 && aIndex === -1) return 1;
    // 如果 a 和 b 的 id 都在 ids 中，按 ids 的顺序排序
    return aIndex - bIndex;
  });
};
export const useGamesData = () => {
  const games = ref<
    (Game & {
      types: string[];
    })[]
  >(
    (LocalStorage.getItem("games") || []).map((game: Game) => ({
      ...game,
      types: (game.type || "").split(",").filter(Boolean),
    }))
  );
  const newGames = computed(() =>
    unref(games).filter((game) => game.newGame == "1")
  );
  const hotGames = computed(() =>
    unref(games).filter((game) => game.hotgame == "1")
  );
  const classicGames = computed(() =>
    unref(games).filter((game) => game.classicgames == "1")
  );

  // puzzleGames
  const puzzle_sortIds = ["58", "51", "32", "63", "62"];
  const puzzleGames = computed(() => {
    const data = unref(games).filter((game) => game.types.includes("Puzzle"));
    return sortGamesByIds(data, puzzle_sortIds);
  });
  // actionGames
  const action_sortIds = ["54", "42", "57", "48", "7"];
  const actionGames = computed(() => {
    const data = unref(games).filter((game) => game.types.includes("Action"));
    return sortGamesByIds(data, action_sortIds);
  });
  // sportsGames
  const sports_sortIds = ["56", "35", "36", "34", "50"];
  const sportsGames = computed(() => {
    const data = unref(games).filter((game) => game.types.includes("Sports"));
    return sortGamesByIds(data, sports_sortIds);
  });
  // ShootingGames
  const shooting_sortIds = ["18", "53", "39", "43", "30"];
  const shootingGames = computed(() => {
    const data = unref(games).filter((game) => game.types.includes("Shooting"));
    return sortGamesByIds(data, shooting_sortIds);
  });
  // BeautyGames
  const beauty_sortIds = ["49", "64", "26", "25", "23"];
  const beautyGames = computed(() => {
    const data = unref(games).filter((game) => game.types.includes("Beauty"));
    return sortGamesByIds(data, beauty_sortIds);
  });
  // CasualGames
  const casual_sortIds = ["33", "31", "37", "59", "55"];
  const casualGames = computed(() => {
    const data = unref(games).filter((game) => game.types.includes("Casual"));
    return sortGamesByIds(data, casual_sortIds);
  });

  const typeGameMap: any = {
    Puzzle: puzzleGames,
    Action: actionGames,
    Sports: sportsGames,
    Shooting: shootingGames,
    Beauty: beautyGames,
    Casual: casualGames,
  };

  return {
    games,
    newGames,
    hotGames,
    classicGames,
    puzzleGames,
    actionGames,
    sportsGames,
    shootingGames,
    beautyGames,
    casualGames,
    typeGameMap,
  };
};
